#include <REGX52.H>

sbit LCD_WR=P2^5;
sbit LCD_RS=P2^6;
sbit LCD_EN=P2^7;
#define LCD_DataPort P0
/**
  * @brief  LCD1602私有延时函数
  * @param  无
  * @retval 无
  */
void LCD_Delay1ms(void)	//@11.0592MHz
{
	unsigned char data i, j;

	i = 2;
	j = 199;
	do
	{
		while (--j);
	} while (--i);
}
/**
  * @brief  LCD1602写指令
  * @param  Command 输入指令
  * @retval 无
  */
void LCD_WriteCommand(unsigned char Command)
{
	LCD_RS=0;
	LCD_WR=0;
	LCD_DataPort=Command;
	LCD_EN=1;
	LCD_Delay1ms();
	LCD_EN=0;
	LCD_Delay1ms();
}
/**
  * @brief  LCD1602写数据
  * @param  Data输入数据
  * @retval 无
  */
void LCD_writeData(unsigned char Data)
{
	LCD_RS=1;
	LCD_WR=0;
	LCD_DataPort=Data;
	LCD_EN=1;
	LCD_Delay1ms();
	LCD_EN=0;
	LCD_Delay1ms();
}
/**
  * @brief  LCD1602初始化
  * @param  无
  * @retval 无
  */
void LCD_Init()
{
	LCD_WriteCommand(0x38);
	LCD_WriteCommand(0x0C);
	LCD_WriteCommand(0x06);
	LCD_WriteCommand(0x01);
}
/**
  * @brief  LCD1602设置光标
  * @param  Line 输入行，范围1~2，Column 输入列，范围1~16
  * @retval 无
  */
void LCD_SetAC(unsigned char Line,unsigned char Column)
{
	if(Line==1)
	{
		LCD_WriteCommand(0x80|(Column=Column-1));
	}
	else
	{
		LCD_WriteCommand(0x80|(Column=Column-1)+0x40);
	}
}
/**
  * @brief  LCD1602显示字符
  * @param  Line 输入行，范围1~2，Column 输入列，范围1~16，Char 输入字符
  * @retval 无
  */
void LCD_ShowChar(unsigned char Line,unsigned char Column,unsigned char Char)
{
	LCD_SetAC(Line,Column);
	LCD_writeData(Char);
}
/**
  * @brief  LCD1602显示字符串
  * @param  Line 输入行，范围1~2，Column 输入列，范围1~16，String[] 输入字符串数组首地址
  * @retval 无
  */
void LCD_ShowString(unsigned char Line,unsigned char Column,unsigned char String[])
{
	unsigned char i;
	LCD_SetAC(Line,Column);
	for(i=0;String[i]!='\0';i++)
	{
		LCD_writeData(String[i]);
	}
}
/**
  * @brief  次方计算
  * @param  x，y
  * @retval 无
  */
int LCD_Pow(int x,int y)
{
	unsigned char i;
	int result=1;
	for(i=0;i<y;i++)
	{
		result*=x;
	}
	return result;
}
/**
  * @brief  LCD1602显示数字
  * @param  Line 输入行，范围1~2，Column 输入列，范围1~16，Number 输入数字，范围0~65536，Length  输入显示长度，范围1~16
  * @retval 无
  */
void LCD_ShowNum(unsigned char Line,unsigned char Column,unsigned int Number,unsigned char Length)
{
	unsigned char i;
	LCD_SetAC(Line,Column);
	for(i=Length;i>0;i--)
	{
		LCD_writeData(Number/LCD_Pow(10,i-1)%10+'0');
	}	
}
/**
  * @brief  LCD1602显示有符号数字
  * @param  Line 输入行，范围1~2，Column 输入列，范围1~16，Number 输入数字，范围-32768~32767，Length  输入显示长度，范围1~16
  * @retval 无
  */
void LCD_ShowSignedNum(unsigned char Line,unsigned char Column,int Number,unsigned char Length)
{
	unsigned char i;
	unsigned int Number1;
	LCD_SetAC(Line,Column);
	if(Number>=0)
	{
		LCD_writeData('-');
		Number1=Number;
	}
	else
	{
		LCD_writeData('-');
		Number1=-Number;
	}
	for(i=Length;i>0;i--)
	{
		LCD_writeData(Number1/LCD_Pow(10,i-1)%10+'0');
	}	
}
/**
  * @brief  LCD1602显示十六进制数字
  * @param  Line 输入行，范围1~2，Column 输入列，范围1~16，Number 输入数字，范围0~65536，Length  输入显示长度，范围1~16
  * @retval 无
  */
void LCD_ShowHexNum(unsigned char Line,unsigned char Column,unsigned int Number,unsigned char Length)
{
	unsigned char i;
	unsigned char SingleNumber;
	LCD_SetAC(Line,Column);
	for(i=Length;i>0;i--)
	{
		SingleNumber=Number/LCD_Pow(16,i-1)%16;
		if(SingleNumber<10)
		{
			LCD_writeData(SingleNumber+'0');
		}
		else
		{
			LCD_writeData(SingleNumber%10+'A');
		}	
	}
}
/**
  * @brief  LCD1602显示二进制数字
  * @param  ine 输入行，范围1~2，Column 输入列，范围1~16，Number 输入数字，范围0~65536，Length  输入显示长度，范围1~16
  * @retval 无
  */
void LCD_ShowBinNum(unsigned char Line,unsigned char Column,unsigned int Number,unsigned char Length)
{
	unsigned char i;
	LCD_SetAC(Line,Column);
	for(i=Length;i>0;i--)
	{
		LCD_writeData(Number/LCD_Pow(2,i-1)%2+'0');
	}
}